package com.rzz.overalldemo.config;

import com.rzz.overalldemo.common.RespBean;
import com.rzz.overalldemo.execption.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @description:
 * @author: ruanzhizhi
 * @date: 2021/7/15 16:59
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    public RespBean handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(">>> found an MethodArgumentNotValidException", e);
        return RespBean.error("字段验证错误, 请完善后重试!", HttpStatus.BAD_REQUEST.value());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.OK)
    public RespBean handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error(">>> found an HttpRequestMethodNotSupportedException", e);
        return RespBean.error("请求方法不匹配, 请重试!", HttpStatus.BAD_REQUEST.value());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(HttpStatus.OK)
    public RespBean handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error(">>> found an HttpMessageNotReadableException", e);
        return RespBean.error("缺失请求主体, 请重试!", HttpStatus.BAD_REQUEST.value());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.OK)
    public RespBean handleNoHandlerFoundException(NoHandlerFoundException e) {
        log.error(">>> found an NoHandlerFoundException", e);
        return RespBean.error("资源不存在, 请重试!", HttpStatus.NOT_FOUND.value());
    }


    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public RespBean handleGlobalException(Exception e) {
        RespBean result = handleException(e);
        if (e instanceof ServiceException) {
            result.setCode(((ServiceException) e).getCode());
        } else {
            result.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
        return result;
    }

    private RespBean handleException(Throwable t) {
        log.error(">>> found an Exception", t);
        Throwable root = getRootCause(t);
        return RespBean.error(root.getMessage());
    }

    public static Throwable getRootCause(Throwable t) {
        Throwable cause = t.getCause();
        if (cause == null) {
            return t;
        } else {
            return getRootCause(cause);
        }
    }




}
